package cn.fl.grpcfg.controller;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.logger.eum.LogExceptionEnum;
import cn.cmm.base.framework.logger.eum.LogOperEnum;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.DateUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.customer.service.ICstBscInfoService;
import cn.fl.customer.vo.CstBscInfoVO;
import cn.fl.framework.base.constant.WebBaseConstant;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.grpcfg.service.ISplGrpPrjBizViewService;
import cn.fl.grpcfg.vo.SplGrpPrjBizViewVO;
import cn.fl.project.condition.PrjCstBscInfoQueryCondition;
import cn.fl.project.service.IPrjCstBscInfoService;
import cn.fl.project.vo.PrjCstBscInfoVO;
import cn.fl.spdealer.service.ISplGroupService;
import cn.fl.spdealer.vo.SplGroupVO;
import com.alibaba.fastjson.JSON;
import fl.constant.facade.framework.constant.BusinessIdConstant;
import fl.customer.facade.customer.condition.CstBscInfoQueryCondition;
import fl.spl.facade.framework.constant.SplSystemConstant;
import fl.spl.facade.grpcfg.condition.SplGrpPrjBizViewQC;
import fl.spl.facade.lnkcomp.api.IPrjLnkCompRlPsnQueryApiService;
import fl.spl.facade.lnkcomp.condition.PrjLnkCompRlPsnQC;
import fl.spl.facade.lnkcomp.dto.PrjLnkCompRlPsnDTO;
import fl.spl.facade.spl.api.ISplInfoApiService;
import fl.spl.facade.spl.condition.SplRlPsnQueryCondition;
import fl.spl.facade.spl.dto.SplRlPsnDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import sp.dealer.facade.dealer.api.ISplGroupApiService;
import sp.dealer.facade.dealer.condition.SplGroupQC;
import sp.dealer.facade.dealer.dto.SplGroupDTO;
import sp.dealer.facade.framework.enums.SplGroupSplTypCdEnum;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @Description: 集团经销商挂靠业务数据视图 控制层
 */
@Controller
@RequestMapping("/grpcfg/splgrpprjbizview")
public class SplGrpPrjBizViewController extends BaseController {

	/**
	 * SplGrpPrjBizView 服务层BEAN
	 */
	@Autowired
	private ISplGrpPrjBizViewService splGrpPrjBizViewService;
	
	@Autowired
	private ISplGroupService splGroupService;
	
	@Autowired
	private ISplGroupApiService splGroupApiService ;
	
	@Autowired
	private IPrjCstBscInfoService prjCstBscInfoService;

	@Autowired
	private ISplInfoApiService splInfoApiService;
	
	@Autowired
	private IPrjLnkCompRlPsnQueryApiService prjLnkCompRlPsnQueryApiService ;
	
	@Autowired
	private ICstBscInfoService cstBscInfoService ;
	
	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService ;
	/**
	 * 业务统计页面
	 */
	@RequestMapping(value = "/toGrpMemberDataByPage.do", method = { RequestMethod.GET})
	public ModelAndView toGrpMemberDataByPage(HttpServletRequest request, HttpServletResponse response) {
		String splTypCd = request.getParameter("splTypCd");
		String groupId = request.getParameter("groupId");
		ModelAndView view = new ModelAndView("/spdealer/splAnalysis/splAnalysis");
		view.addObject("groupId",groupId);
		view.addObject("splTypCd",splTypCd);
		BaseLogUtils.newWebLogger("业务统计页面").setLogOperType(LogOperEnum.REQ)
				.setSysCd(CurrentThreadContext.getCurrentSysCd())
				.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
				.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
				.setWarning(false)
				.info();
		return view;
	}
	
	/**
	 * 合作对象页面
	 */
	@RequestMapping(value = "/toSplGrpMembDevAmtRank.do", method = { RequestMethod.GET})
	public ModelAndView toSplGrpMembDevAmtRank(HttpServletRequest request, HttpServletResponse response) {
		String splTypCd = request.getParameter("splTypCd");
		String groupId = request.getParameter("groupId");
		ModelAndView view = new ModelAndView("/spdealer/splAnalysis/splGrpMembDevAmtRank");
		view.addObject("groupId",groupId);
		view.addObject("splTypCd",splTypCd);
		BaseLogUtils.newWebLogger("跳转到列表页面").setLogOperType(LogOperEnum.REQ)
				.setSysCd(CurrentThreadContext.getCurrentSysCd())
				.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
				.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
				.setWarning(false)
				.info();
		return view;
	}
	/**
	 * 到列表页面
	 */
	@RequestMapping(value = "/toListPage.do", method = { RequestMethod.GET})
	public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView("grpcfg/splGrpPrjBizView/splGrpPrjBizViewList");
		BaseLogUtils.newWebLogger("到修改页面").setLogOperType(LogOperEnum.REQ)
				.setSysCd(CurrentThreadContext.getCurrentSysCd())
				.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
				.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
				.setWarning(false)
				.info();
		return view;
	}
	
	/**
	 * 到新增修改页面
	 */
	@RequestMapping(value = "/toAddPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toAddPage(HttpServletRequest request, HttpServletResponse response) {

		ModelAndView view = new ModelAndView("grpcfg/splGrpPrjBizView/splGrpPrjBizViewAdd");
		
		return view;
	}

	/**
	 * 到修改页面
	 */
	@RequestMapping(value = "/toModifyPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toModifyPage(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		ModelAndView view = new ModelAndView("grpcfg/splGrpPrjBizView/splGrpPrjBizViewModify");
		view.addObject("id", id);
		return view;
	}
	
	/**
	 * 到详情页面
	 */
	@RequestMapping(value = "/toDetailPage.do", method = { RequestMethod.GET})
	public ModelAndView toDetailPage(HttpServletRequest request, HttpServletResponse response) {

		String id = request.getParameter("id");
		ModelAndView view = new ModelAndView("grpcfg/splGrpPrjBizView/splGrpPrjBizViewDetail");
		view.addObject("id", id);
		BaseLogUtils.newWebLogger("到修改页面").setLogOperType(LogOperEnum.REQ)
				.setSysCd(CurrentThreadContext.getCurrentSysCd())
				.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
				.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
				.setWarning(false)
				.info();
		return view;
	}

	/**
	 * 业务统计
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initList.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initList(HttpServletRequest request, HttpServletResponse response) {

		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		String groupId = request.getParameter("groupId");
		String splTypCd = request.getParameter("splTypCd");
		String prjTypCd = request.getParameter("prjTypCd");
		String tmTypCd = request.getParameter("tmTypCd");
		String startTm = request.getParameter("startTm");
		String endTm = request.getParameter("endTm");
		ListPageVO<SplGrpPrjBizViewVO> listPage = new ListPageVO<SplGrpPrjBizViewVO>();

		if (StringUtils.isEmpty(groupId)) {
			BaseLogUtils.newWebLogger("集团ID不能为空!").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.warn(LogExceptionEnum.COMMON_CODE_0002);
			return listPage;
		}
		if(StringUtils.isEmpty(splTypCd)){
			BaseLogUtils.newWebLogger("公司类型不能为空!").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.warn(LogExceptionEnum.COMMON_CODE_0002);
			return listPage;
		}
		SplGroupQC groupQC = new SplGroupQC();
		groupQC.setSplGroupId(Long.valueOf(groupId));
		groupQC.setSplTypCd(Integer.valueOf(splTypCd));
		List<SplGroupVO> splGroupVOs = this.splGroupService.searchList(groupQC);
		if(CollectionUtils.isEmpty(splGroupVOs)){
			BaseLogUtils.newWebLogger("集团成员为空!").setLogOperType(LogOperEnum.RESP)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.warn(LogExceptionEnum.COMMON_CODE_0002);
			return listPage;
		}
		List<Long> idList = new ArrayList<>();
		for (SplGroupVO splGroupVO : splGroupVOs) {
			idList.add(splGroupVO.getSplId());
		}
		Pager page = new Pager();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}else{
			page.setCurrentPage(1);
		}
		if (StringUtils.isNotEmpty(pageSize)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}else{
			page.setEveryPage(10);
		}
		SplGrpPrjBizViewQC qc = new SplGrpPrjBizViewQC();
		if(StringUtils.isNotEmpty(splTypCd)){
			qc.setSplTypCd(Integer.valueOf(splTypCd));
			if(SplGroupSplTypCdEnum.LNKCOMP.getSplTypCd().toString().equals(splTypCd)){
				qc.setLnkCompIdList(idList);
			}else if(SplGroupSplTypCdEnum.SPL.getSplTypCd().toString().equals(splTypCd)){
				qc.setSplIdList(idList);
			}
		}
		if (StringUtils.isNotEmpty(prjTypCd)) {
			if("ALL".equals(prjTypCd)){
				List<String> prjTypCdList = new ArrayList<String>();
				prjTypCdList.add("CTYW");
				prjTypCdList.add("CPYW");
				prjTypCdList.add("CDDYW");
				qc.setPrjTypCdList(prjTypCdList);
			}else{
				qc.setPrjTypCd(prjTypCd);
			}
		}
		if (StringUtils.isNotEmpty(tmTypCd)) {
			if ("0".equals(tmTypCd)) {
				qc.setMaxLsDtStr(endTm);
				qc.setMinLsDtStr(startTm);
			} else {
				Date endTmDate = new Date();
				Date startTmDate = DateUtils.getDateAddDate(endTmDate, 2, -Integer.valueOf(tmTypCd));
				qc.setMaxLsDtStr(DateUtils.formatDateToString(endTmDate, DateUtils.YYYY_MM_DD));
				qc.setMinLsDtStr(DateUtils.formatDateToString(startTmDate, DateUtils.YYYY_MM_DD));
			}
		}
		page.setCondition(qc);

		//TODO SOMETHING
		Pager pager = new Pager();
		pager = this.splGrpPrjBizViewService.searchListPage(page);
		List<SplGrpPrjBizViewVO> list = (List<SplGrpPrjBizViewVO>) pager.getResultList();
		if(CollectionUtils.isNotEmpty(list)){
			SplGroupDTO splGroupDTO = this.splGroupApiService.selectById(Long.valueOf(groupId));
			String splNm = null;
			if(splGroupDTO != null){
				splNm = splGroupDTO.getSplName();
			}
			for (SplGrpPrjBizViewVO splGrpPrjBizViewVO : list) {
				splGrpPrjBizViewVO.setSplNm(splNm);
			}
		}
		listPage.setRows(list);
		listPage.setTotal(pager.getTotalCount());
		return listPage;
	}
	
	/**
	 * 合作对象
	 */
	@RequestMapping(value = "/searchSplGrpMembDevAmtRank.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object searchSplGrpMembDevAmtRank(HttpServletRequest request, HttpServletResponse response) {

		String groupId = request.getParameter("groupId");
		String splTypCd = request.getParameter("splTypCd");
		ListPageVO<SplGrpPrjBizViewVO> listPage = new ListPageVO<SplGrpPrjBizViewVO>();

		SplGroupQC groupQC = new SplGroupQC();
		if (StringUtils.isNotEmpty(groupId)) {
			groupQC.setSplGroupId(Long.valueOf(groupId));
		}
		if(StringUtils.isNotEmpty(splTypCd)){
			groupQC.setSplTypCd(Integer.valueOf(splTypCd));
		}

		List<SplGroupVO> splGroupVOs = this.splGroupService.searchList(groupQC);
		if (CollectionUtils.isEmpty(splGroupVOs)) {
			BaseLogUtils.newWebLogger("集团成员为空!").setLogOperType(LogOperEnum.RESP)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.warn(LogExceptionEnum.COMMON_CODE_0002);
			return listPage;
		}
		List<Long> idList = new ArrayList<>();
		for (SplGroupVO splGroupVO : splGroupVOs) {
			idList.add(splGroupVO.getSplId());
		}
		
		SplGrpPrjBizViewQC qc = new SplGrpPrjBizViewQC();
		if(StringUtils.isNotEmpty(splTypCd)){
			qc.setSplTypCd(Integer.valueOf(splTypCd));
			if(SplGroupSplTypCdEnum.LNKCOMP.getSplTypCd().toString().equals(splTypCd)){
				qc.setLnkCompIdList(idList);
			}else if(SplGroupSplTypCdEnum.SPL.getSplTypCd().toString().equals(splTypCd)){
				qc.setSplIdList(idList);
			}
		}
		/*List<String> prjTypCdList = new ArrayList<String>();
		prjTypCdList.add("CTYW");
		prjTypCdList.add("CPYW");
		prjTypCdList.add("CDDYW");
		qc.setPrjTypCdList(prjTypCdList);*/
		List<SplGrpPrjBizViewVO> list = this.splGrpPrjBizViewService.searchSplGrpMembDevAmtRank(qc);
		if(CollectionUtils.isNotEmpty(list)){
			double sum = 0;
			for (SplGrpPrjBizViewVO splGrpPrjBizViewVO : list) {
				sum = sum + (double)splGrpPrjBizViewVO.getTotalDevAmt();
			}
			for (SplGrpPrjBizViewVO splGrpPrjBizViewVO : list) {
				splGrpPrjBizViewVO.setSumDevAmt((long)sum);
			}
		}
		listPage.setRows(list);
		return listPage;
	}
	
	/**
	 * 新增
	 */
	@RequestMapping(value = "/add.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object add(HttpServletRequest request, HttpServletResponse response) throws BaseException {

		String name = request.getParameter("name");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "新增成功!");
		SplGrpPrjBizViewVO entity = null;
		try {
			entity = new SplGrpPrjBizViewVO();
			//TODO ADD PARAM
			Long id = this.splGrpPrjBizViewService.add(entity);
			result.setData(id);
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("系统错误,请联系管理员!");
		}

		return result;
	}

	/**
	 * 修改
	 */
	@RequestMapping(value = "/modify.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object modify(HttpServletRequest request, HttpServletResponse response) throws BaseException {

		String id = request.getParameter("id");
		String name = request.getParameter("name");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "修改成功!");
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("数据不存在!");
			return result;
		}
		SplGrpPrjBizViewVO entity = null;
		try {
			entity = new SplGrpPrjBizViewVO();
			entity.setId(Long.valueOf(id));
			//entity.setName(name);
			//TODO ADD PARAM
			boolean success = this.splGrpPrjBizViewService.modify(entity);
			if(!success){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("修改失败!");
			}
		} catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("系统错误,请联系管理员!");
		}
		
		return result;
	}

	/**
	 * 删除数据
	 */
	@RequestMapping(value = "/removeById.do", method = {RequestMethod.POST })
	@ResponseBody
	public DataResultVO removeById(HttpServletRequest request, HttpServletResponse response) throws BaseException {

		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "删除成功!");
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("主键不能为空!");
			return result;
		}
		try {
			this.splGrpPrjBizViewService.removeById(Long.valueOf(id));
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
		} catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("系统错误,请联系管理员!");
		}
		
		return result;
	}

	/**
	 * 得到一条记录
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getById.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object getById(HttpServletRequest request, HttpServletResponse response) throws BaseException {

		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "操作成功!");
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("主键不能为空!");
			return result;
		}
		
		try {
			SplGrpPrjBizViewVO vo = this.splGrpPrjBizViewService.selectById(Long.valueOf(id));
			if(null == vo){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("数据不存在!");
			}
			result.setData(vo);
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
		} catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("系统错误,请联系管理员!");
		}
		
		return result;
	}
	
	/**
	 * 初始化列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initAsSureLoanList.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initAsSureLoanList(HttpServletRequest request, HttpServletResponse response) {
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		String groupId = request.getParameter("groupId");
		String cstRlCd = request.getParameter("cstRlCd");
		String dataId = request.getParameter("dataId");
		String dataNm = request.getParameter("dataNm");
		String cstNm = request.getParameter("cstNm");
		ListPageVO<SplGrpPrjBizViewVO> listPage = new ListPageVO<SplGrpPrjBizViewVO>();

        if (StringUtils.isEmpty(groupId)) {
			BaseLogUtils.newWebLogger("集团ID不能为空!").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.warn(LogExceptionEnum.COMMON_CODE_0002);
            return listPage;
        }
        if(StringUtils.isEmpty(cstRlCd)){
			BaseLogUtils.newWebLogger("客户关系类型不能为空!").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.warn(LogExceptionEnum.COMMON_CODE_0002);
            return listPage;
        }

        //根据条件查询集团所有成员
        SplGroupQC qc = new SplGroupQC();
        //条件：成员ID
        if(StringUtils.isNotEmpty(dataId)){
            qc.setId(Long.valueOf(dataId));
        }
        //条件：成员名称
        if(StringUtils.isNotEmpty(dataNm)){
            qc.setSplName(dataNm);
        }
        qc.setSplGroupId(Long.valueOf(groupId));
        qc.setIsValid(BaseConstant.IS_YESNO_YES);
        qc.setIsDel(BaseConstant.IS_YESNO_NO);

        List<SplGroupDTO> searchList = splGroupApiService.searchList(qc);
        if(CollectionUtils.isEmpty(searchList)){
			BaseLogUtils.newWebLogger("集团成员为空!").setLogOperType(LogOperEnum.RESP)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.warn(LogExceptionEnum.COMMON_CODE_0002);
            return listPage;
        }

        Pager page = new Pager();

        if (StringUtils.isNotEmpty(currentPage)) {
            page.setCurrentPage(Integer.parseInt(currentPage));
        }
        if (StringUtils.isNotEmpty(pageSize)) {
            page.setEveryPage(Integer.parseInt(pageSize));
        }

        ArrayList<Long> splIds = new ArrayList<>();
        ArrayList<Long> lnkIds = new ArrayList<>();
        ArrayList<Long> cstIds = new ArrayList<>();
        List<String> cstNms = new ArrayList<>();
        for (SplGroupDTO dto : searchList) {
			cstNms.add(dto.getSplName());
            if(StringUtils.isNotEmpty(dto.getSplTypCd()) && dto.getSplTypCd() == 2){
                splIds.add(dto.getSplId());
            }else if(StringUtils.isNotEmpty(dto.getSplTypCd()) && dto.getSplTypCd() == 3){
                lnkIds.add(dto.getSplId());
            }
        }

        //根据供应商ID查询供应商联系人
        List<SplRlPsnDTO> splRlPsnList = new ArrayList<>();
        //根据挂靠公司ID查询挂靠公司联系人
        List<PrjLnkCompRlPsnDTO> lnkRlPsnList = new ArrayList<>();
        //证件号集合
        List<String> certNoList = new ArrayList<>();

        if(CollectionUtils.isNotEmpty(splIds)){
            SplRlPsnQueryCondition splRlPsnQC = new SplRlPsnQueryCondition();
            splRlPsnQC.setSplIdList(splIds);
            splRlPsnQC.setIsDel(BaseConstant.IS_YESNO_NO);
            splRlPsnList = this.splInfoApiService.searchPrdSplRlPsnList(splRlPsnQC , CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
			BaseLogUtils.newWebLogger("查询供应商联系人集合").setLogOperType(LogOperEnum.RESP)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.setKeys("splRlPsnList", JSON.toJSONString(splRlPsnList))
					.info();
        }
        if(CollectionUtils.isNotEmpty(lnkIds)){
            PrjLnkCompRlPsnQC lnkRlPsnQC = new PrjLnkCompRlPsnQC();
            lnkRlPsnQC.setIsDel(BaseConstant.IS_YESNO_NO);
            lnkRlPsnQC.setLnkCompIdList(lnkIds);
            lnkRlPsnList = this.prjLnkCompRlPsnQueryApiService.searchLists(lnkRlPsnQC);
			BaseLogUtils.newWebLogger("查询挂靠公司联系人集合").setLogOperType(LogOperEnum.RESP)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.setKeys("lnkRlPsnList", JSON.toJSONString(lnkRlPsnList))
					.info();
        }
        if(CollectionUtils.isNotEmpty(splRlPsnList)){
            for (SplRlPsnDTO dto : splRlPsnList) {
                if(StringUtils.isNotEmpty(dto.getCertNo())){
                    certNoList.add(dto.getCertNo());
                }
            }
        }
        if(CollectionUtils.isNotEmpty(lnkRlPsnList)){
            for (PrjLnkCompRlPsnDTO dto : lnkRlPsnList) {
                if(StringUtils.isNotEmpty(dto.getCertNo())){
                    certNoList.add(dto.getCertNo());
                }
            }
        }

		//根据客户名称查询客户集合
		if(CollectionUtils.isNotEmpty(cstNms)){
			CstBscInfoQueryCondition cstBscQC = new CstBscInfoQueryCondition();
			//客户名称分组查询客户信息
			List<List<String>> grpMemberNmsList = null ;
			List<CstBscInfoVO> searchListByCon = null;
			if(cstNms.size() > 1000){
				int recycleTimes = cstNms.size() % 800 == 0 ? cstNms.size()/800 : cstNms.size()/800 + 1 ;
				grpMemberNmsList = new ArrayList<>(recycleTimes);
				List<String> memberNms = null ;
				int index = 0;
				for(String memberNm : cstNms){
					++index;
					if(index == 1 || index % 800 == 0 ){
						if(index % 800 == 0){
							grpMemberNmsList.add(memberNms);
						}
						memberNms = new ArrayList<>(800);
					}
					memberNms.add(memberNm);
					if(index == cstNms.size()){
						grpMemberNmsList.add(memberNms);
					}
				}
				for(List<String> memberNmList : grpMemberNmsList){
					cstBscQC.setCustNmAlls(memberNmList);
					if(null == searchListByCon){
						searchListByCon = this.cstBscInfoService.searchListByCon(cstBscQC);
					}else{
						searchListByCon.addAll(this.cstBscInfoService.searchListByCon(cstBscQC));
					}
				}
			}else{
				cstBscQC.setCustNmAlls(cstNms);
				searchListByCon = this.cstBscInfoService.searchListByCon(cstBscQC);
			}

			if(CollectionUtils.isNotEmpty(searchListByCon)){
				for (CstBscInfoVO cstBscInfoVO : searchListByCon) {
					cstIds.add(cstBscInfoVO.getId());
				}
			}
		}
        //根据证件号查询客户集合
        if(CollectionUtils.isNotEmpty(certNoList)){
            CstBscInfoQueryCondition cstBscQC = new CstBscInfoQueryCondition();
			//客户身份证号分组查询客户信息
			List<List<String>> grpMemberCertNosList = null ;
			List<CstBscInfoVO> cstBscInfoVOList = null ;
			if(certNoList.size() > 1000){
				int recycleTimes = certNoList.size() % 800 == 0 ? certNoList.size()/800 : certNoList.size()/800 + 1 ;
				grpMemberCertNosList = new ArrayList<>(recycleTimes);
				List<String> certNos = null ;
				int index = 0;
				for(String certNo : certNoList){
					++index;
					if(index == 1 || index % 800 == 0 ){
						if(index % 800 == 0){
							grpMemberCertNosList.add(certNos);
						}
						certNos = new ArrayList<>(800);
					}
					certNos.add(certNo);
					if(index == certNoList.size()){
						grpMemberCertNosList.add(certNos);
					}
				}
				for(List<String> memberCertNoList : grpMemberCertNosList){
					cstBscQC.setCertNoList(memberCertNoList);
					if(null == cstBscInfoVOList){
						cstBscInfoVOList = this.cstBscInfoService.searchListByCon(cstBscQC);
					}else{
						cstBscInfoVOList.addAll(this.cstBscInfoService.searchListByCon(cstBscQC));
					}
				}
			}else{
				cstBscQC.setCertNoList(certNoList);
				cstBscInfoVOList = cstBscInfoService.searchListByCon(cstBscQC);
			}

			if(CollectionUtils.isNotEmpty(cstBscInfoVOList)){
				for (CstBscInfoVO cstBscInfoVO : cstBscInfoVOList) {
					cstIds.add(cstBscInfoVO.getId());
				}
			}
        }



        //根据客户ID集合查询担保及贷款信息
        if(CollectionUtils.isNotEmpty(cstIds)){
			SplGrpPrjBizViewQC condition = new SplGrpPrjBizViewQC();
            condition.setCstIds(cstIds);
            condition.setCstRlCd(Integer.parseInt(cstRlCd));
			if(StringUtils.isNotEmpty(cstNm)){
				condition.setCstNm(cstNm);
			}
            page.setCondition(condition);
            Pager pager = this.splGrpPrjBizViewService.searchAsSureListPage(page);

            List<SplGrpPrjBizViewVO> list = (List<SplGrpPrjBizViewVO>) pager.getResultList();
			BaseLogUtils.newWebLogger("分页查询担保或贷款信息").setLogOperType(LogOperEnum.RESP)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.setKeys("list", JSON.toJSONString(list))
					.info();
            if(CollectionUtils.isNotEmpty(list)){
                Map<String, String> prjTypMap = this.sysDictionaryApiService.searchGroupToMapAll(WebBaseConstant.SYS_SYS_CD, WebBaseConstant.SYS_P_ORG_CD, WebBaseConstant.PRJ_TYP_CODE_GRP_CD);
                List<Long> prjIds = new ArrayList<>();
                for (SplGrpPrjBizViewVO splGrpPrjBizViewVO : list) {
                    splGrpPrjBizViewVO.setPrjTypCdNm(prjTypMap.get(splGrpPrjBizViewVO.getPrjTypCd()));
                    prjIds.add(splGrpPrjBizViewVO.getPrjId());
                }
                if(cstRlCd.equals("2")){
                    Map<Long, PrjCstBscInfoVO> map = new HashMap<>();
                    PrjCstBscInfoQueryCondition prjCondition = new PrjCstBscInfoQueryCondition();
                    prjCondition.setCstRlTyp("1");
                    prjCondition.setPrjIds(prjIds);
                    List<PrjCstBscInfoVO> searchList2 = this.prjCstBscInfoService.searchList(prjCondition);
                    if(CollectionUtils.isNotEmpty(searchList2)){
                        for (PrjCstBscInfoVO prjCstBscInfoVO : searchList2) {
                            map.put(prjCstBscInfoVO.getPrjId(), prjCstBscInfoVO);
                        }
                    }
                    if(!map.isEmpty()){
                        for (SplGrpPrjBizViewVO splGrpPrjBizViewVO : list) {
                            splGrpPrjBizViewVO.setDataNm(map.get(splGrpPrjBizViewVO.getPrjId()).getCstNm());
                            splGrpPrjBizViewVO.setCertNo(map.get(splGrpPrjBizViewVO.getPrjId()).getCertNo());
                        }
                    }
                }
            }
			listPage.setRows(list);
            listPage.setTotal(pager.getTotalCount());
        }else{
			BaseLogUtils.newWebLogger("集团客户为空!").setLogOperType(LogOperEnum.RESP)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.warn(LogExceptionEnum.COMMON_CODE_0002);
        }
        return listPage;
	}
	/**
	 * 初始化列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/toLoanList.do", method = { RequestMethod.GET})
	@ResponseBody
	public ModelAndView toLoanList(HttpServletRequest request, HttpServletResponse response) {
		String groupId = request.getParameter("groupId");
		ModelAndView view = new ModelAndView("grpcfg/splGrpPrjBiz/splGrpPrjBizList");
		view.addObject("groupId", groupId);
		return view ;
	}
	/**
	 * 初始化列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/toAsSureList.do", method = { RequestMethod.GET})
	@ResponseBody
	public ModelAndView toAsSureList(HttpServletRequest request, HttpServletResponse response) {
		String groupId = request.getParameter("groupId");
		String cstRlCd = request.getParameter("cstRlCd");
		ModelAndView view = new ModelAndView("grpcfg/splGrpPrjBiz/splAsSureList");
		view.addObject("groupId", groupId);
		view.addObject("vcstRlCd",cstRlCd);
		return view ;
	}
	/**
	 * 到集团贷款及担保页
	 */
	@RequestMapping(value = "/tosplGrpAnalysisPage.do", method = { RequestMethod.GET})
	public ModelAndView toLnkCompAnalysisPage(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView("/grpcfg/splGrpPrjBiz/splGrpAnalysisPage");
		return view;
	}
}

